﻿using System;
using System.Collections.Generic;
using System.Configuration;

using ApplicationBoilerplate.DependencyInjection;

using mvcForum.Core.Interfaces;

namespace MvcForum.Hosting.Azure {

	/// <summary>
	/// Dependency builder for Azure attachment storage.
	/// </summary>
	public class StorageBuilder : IDependencyBuilder {

		/// <summary>
		/// Configure method of the AzureStorageBuilder class.
		/// Here we wire up everything needed for MVC Forum to use an Azure blob storage for attachments.
		/// </summary>
		/// <param name="container">The dependency container.</param>
		public void Configure(IDependencyContainer container) {
			// Let's make sure this is the only attachment storage provider around!
			container.UnRegister<IAttachmentStorage>();
			// We're going to run this is singleton mode, so create it!
			// This is for Azure, we better get the name of the container from the regular web.config
			IAttachmentStorage storage = new AttachmentStorage(ConfigurationManager.AppSettings["AzureStorageCatalog"]);
			// Let's register the Azure storage provider!
			container.RegisterSingleton<IAttachmentStorage>(storage);
		}

		/// <summary>
		/// Method for validating all requirements for the dependency builder are present.
		/// </summary>
		/// <param name="feedback">Return any warning/fatal feedback in this list.</param>
		public void ValidateRequirements(IList<ApplicationRequirement> feedback) {
			// Get the bucket/catalogue!
			String container = ConfigurationManager.AppSettings["AzureStorageCatalog"];
			// Is it empty?
			if (String.IsNullOrWhiteSpace(container)) {
				// This will make Lucene fail, let's return a warning!
				feedback.Add(new ApplicationRequirement { Level = RequirementLevel.Warning, Feedback = "You need an app setting for 'AzureStorageCatalog' in the web.config file when using Azure's blob storage for Lucene storage!" });
			}
		}
	}
}